﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.DynamicData;
using System.ComponentModel.DataAnnotations;

/// <summary>
/// By default, Dynamic Data doesn't blindly display all exceptions in the page,
/// as some database exceptions may contain sensitive info.  Instead, it only displays
/// ValidationExceptions.
/// However, in some cases you need to display other exceptions as well.  This code
/// shows how to achieve this.
/// </summary>
public class MyDynamicValidator : DynamicValidator {

    protected override void ValidateException(Exception exception) {
        // If it's not already an exception that DynamicValidator looks at
        if (!(exception is IDynamicValidatorException) && !(exception is ValidationException)) {
            // Find the most inner exception
            while (exception.InnerException != null) {
                exception = exception.InnerException;
            }

            //Case of UNIQUE KEY
            if (exception.ToString().Contains("Violation of UNIQUE KEY constraint"))
                exception = new ValidationException("Lỗi xảy ra! Mã HS đã tồn tại trong hệ thống",exception);

            //Case of The DELETE statement conflicted with the REFERENCE constraint "FK_Product_ProductCategoryID_ParentProductCategoryID_ProductCategoryID"
            if (exception.ToString().Contains("The DELETE statement conflicted with the REFERENCE constraint"))
                exception = new ValidationException("Lỗi xảy ra! Thao tác xóa vi phạm luật tham chiếu [Products]",exception);

            //Case of The DELETE statement conflicted with the SAME TABLE REFERENCE constraint "FK_ProductCategory_ProductCategory_ParentProductCategoryID_ProductCategoryID"
            if (exception.ToString().Contains("The DELETE statement conflicted with the SAME TABLE REFERENCE constraint"))
                exception = new ValidationException("Lỗi xảy ra! Thao tác xóa vi phạm luật tham chiếu [ProductCategories]", exception);

            //Case of The DELETE statement conflicted with the SAME TABLE REFERENCE constraint "FK_ProductCategory_ProductCategory_ParentProductCategoryID_ProductCategoryID"
            if (exception.ToString().Contains("Entities may have been modified or deleted since entities were loaded"))
                exception = new ValidationException("Lỗi xảy ra! Mẫu tin nầy đã được chỉnh sửa hoặc xóa", exception);

            // Wrap it in a ValidationException so the base code doesn't ignore it
            if (ExceptionShouldBeDisplayedInPage(exception)) {
                exception = new ValidationException(null, exception);
            }
        }

        // Call the base on the (possibly) modified exception
        base.ValidateException(exception);
    }

    private bool ExceptionShouldBeDisplayedInPage(Exception e) {
        // This is where you may want to add logic that looks at the exception and
        // decides whether it should indeed be shown in the page
        return true;
    }
}


